package com.cmccgz.udacs_landlord_webapp.main.warning.presenters;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.bean.WarningItem;
import com.cmccgz.udacs_landlord_webapp.main.warning.contracts.WarningListContract;

import java.util.List;

import rx.Subscriber;

/**
 * Created by hzx07 on 2017/8/11.
 */

public class WarningListPresenter implements WarningListContract.Presenter {
    private WarningListContract.View view;

    public WarningListPresenter(WarningListContract.View view) {
        this.view = view;
    }

    @Override
    public void getDatas(String q_word, String o_cond, String f_cond, final String page) {
       /* final List<WarningItem>datas=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String w_id = (Integer.parseInt(page) - 1) * 10 + i +1+ "";
            String w_state =  ((Integer.parseInt(page) - 1) * 10 + i+1)%4+1+"";
            datas.add(new WarningItem(w_id,"南洲街"+w_id+"号",new Date().getTime()+"",w_state));
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ((Fragment) view).getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if ("1".equals(page)) {
                            view.refreshSuccess(datas);
                        }else{
                            view.loadMoreSuccess(datas);
                        }
                    }
                });
            }
        }).start();*/
        final boolean isRefresh = "1".equals(page);
        ApiService.getWarningList(q_word,o_cond,f_cond,page).subscribe(new Subscriber<ResultBean<List<WarningItem>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (isRefresh) {
                    view.refreshDatasError("加载失败！");
                }else{
                    view.loadDatasError("加载失败！");
                }
            }

            @Override
            public void onNext(ResultBean<List<WarningItem>> listResultBean) {
                if ("0".equals(listResultBean.rsmsg.rscode)) {
                    if (isRefresh){
                        view.refreshSuccess(listResultBean.resp);
                    }else{
                        view.loadMoreSuccess(listResultBean.resp);
                    }
                }else{
                    view.loadDatasError(listResultBean.rsmsg.rsdesc);
                }
            }
        });
    }
}
